package com.guozcc.bootweb.platform.config.filter;

import com.guozcc.bootweb.platform.common.AutoHttpServletRequestWrapper;
import com.guozcc.bootweb.platform.common.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Component
@WebFilter(urlPatterns = "/*",filterName = "RequestWrapperFilter")
public class RequestWrapperFilter implements Filter {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //设置日志traceId
        String traceId = DateUtils.getCurrentDateStr("yyyyMMddHHmmssSSS")+"_"+UUID.randomUUID().toString().replace("-","");
        MDC.put("traceId", traceId);
//        DispatcherServlet

//        AutoHttpServletRequestWrapper autoHttpServletRequestWrapper = new AutoHttpServletRequestWrapper((HttpServletRequest) servletRequest);
//        filterChain.doFilter(autoHttpServletRequestWrapper,servletResponse);

//        ContentCachingRequestWrapper contentCachingRequestWrapper = new ContentCachingRequestWrapper((HttpServletRequest) servletRequest);
//        ContentCachingResponseWrapper contentCachingResponseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) servletResponse);
//        filterChain.doFilter(contentCachingRequestWrapper,contentCachingResponseWrapper);
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
